/**
 * 
 */
package com.aat.dxfy.thread.safe1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @date 2016年6月23日 下午1:51:38
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 功能是：
 * @info java spring
 * @update none
 */
public class Test04 {

	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		Count c=new Count(10);
		service.submit(new R1(c));
		service.submit(new R2(c));
		service.shutdown();
	}

}

class R1 implements Runnable {
	private Count c;

	public R1(Count c) {
		this.c = c;
	}

	public void run() {
		for (int i = 0; i < 10; i++) {
			c.addx();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
	}

}

class R2 implements Runnable {
	private Count c;

	public R2(Count c) {
		this.c = c;
	}

	public void run() {
		for (int i = 0; i < 10; i++) {
			c.subx();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
			}
		}
	}

}

class Count {
	private int x;

	public Count(int x) {
		this.x = x;
	}

	public synchronized void addx() {
		x++;
		System.out.println("x++: " + x);
	}

	public synchronized void subx() {
		x--;
		System.out.println("x--: " + x);
	}

}